﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace ALDIOrganizer
{
    public class Contract : IComparable
    {
        internal DataRow row;

        public Contract(DataRow row)
        {
            this.row = row;            
        }

        public Int64 ID
        {
            get { return (Int64)row[0]; }
            set { row[0] = (Int64)value; }
        }

        public Int64 empID
        {
            get { return (Int64)row[1]; }
            set { row[1] = (Int64)value; }
        }

        public DateTime date
        {
            get { return (DateTime)row[2]; }
            set { row[2] = (DateTime)value; }
        }

        public bool Hired
        {
            get { return (bool)row[3]; }
            set { row[3] = (bool)value ? 1 : 0; }
        }        

        public Position role
        {
            get { return ((Int64)row[4]).To<Position>(); }
            set { row[4] = (Int64)value; }
        }
        
        public Int64 contractHrs
        {
            get { return (Int64)row[5]; }
            set { row[5] = (Int64)value; }
        }

        public Int64 holidayAllowance
        {
            get { return (Int64)row[6]; }
            set { row[6] = (Int64)value; }
        }

        public string comments
        {
            get { return (string)row[7]; }
            set { row[7] = (string)value; }
        }

        public string displayColumn
        {
            get { return (string)row[8]; }
            set { row[8] = (string)value; }
        }

        public override string ToString()
        {
            return displayColumn;
        }

        public string FullDescription(bool role, bool hours, bool holidays)
        {
            var result = date.ToShortDateString();
            result += ": ";

            if (Hired)
            {
                if (role) result += this.role.ToShortString() + "; ";
                if (hours) result += contractHrs.ToString() + " hrs; ";
                if (holidays) result += holidayAllowance.ToString() + " holidays; ";
            }
            else result += "terminated"; // not Hired

            if (result.Substring(result.Length - 2, 2) == "; ")
                result = result.Remove(result.Length - 2, 2);

            return result;
        }

        public static Contract[] ToContracts(DataRow[] rows)
        {
            var list = new List<Contract>();
            foreach (var row in rows)
                list.Add(new Contract(row));
            list.Sort();
            return list.ToArray();
        }

        public static void DescribeContracts(Contract[] contracts)
        {
            if (contracts == null
                || contracts.Length == 0)
            {
                return;
            }

            int i = 0;
            Contract previous = null;
            foreach (var contract in contracts)
            {
                if (i++ == 0)
                    contract.displayColumn = contract.FullDescription(true, true, true);
                else
                {
                    var role = previous.role != contract.role;
                    var hours = previous.contractHrs != contract.contractHrs;
                    var hols = previous.holidayAllowance != contract.holidayAllowance;
                    contract.displayColumn = contract.FullDescription(role, hours, hols);
                }
                previous = contract;
            }
        }



        #region IComparable Members

        public int CompareTo(object obj)
        {
            if(obj.GetType() != this.GetType())
                return default(int);
            var other = obj as Contract;
            return this.date.CompareTo(other.date);
        }

        #endregion
    }    
}
